package com.tandashi.springbootstudy01.security.shiro;

import com.tandashi.springbootstudy01.entity.user.User;
import com.tandashi.springbootstudy01.entity.user.WechatUser;
import com.tandashi.springbootstudy01.enums.UserType;
import com.tandashi.springbootstudy01.service.user.WeChatUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashSet;
import java.util.Set;

@Slf4j
public class StudyAuthorizingRealm extends AuthorizingRealm {

    @Autowired
    private WeChatUserService weChatUserService;

    public StudyAuthorizingRealm(WeChatUserService weChatUserService) {
        this.weChatUserService = weChatUserService;
    }

    /**
     * 授权方法
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        StudyPrincipal principal = (StudyPrincipal) principalCollection.getPrimaryPrincipal();
        Set<String> roles = new HashSet<>();
        Integer userType = principal.getUserType();
        if(userType == null || userType == UserType.Unregistered.getCode()){
            roles.add("UNREGISTERED");
        } else if (userType == UserType.Ordinary.getCode()){
            roles.add("ORDINARY");
        }  else if (userType == UserType.Senior.getCode()){
            roles.add("SENIOR");
        }
        return new SimpleAuthorizationInfo(roles);
    }

    /**
     * 认证方法  有个问题 为啥 token 获取的就是 unionid ?
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        String unionid = (String) authenticationToken.getPrincipal();

        StudyPrincipal principal = new StudyPrincipal(unionid);
        WechatUser weChatUser = weChatUserService.findWeChatUser(unionid);
        if (weChatUser == null) {
            throw new AuthenticationException("No wechat user found for unionid " + unionid);
        }

        principal.setName(weChatUser.getNickname());

        User user = weChatUser.getUser();
        if (user != null && user.getId() != null) {
            principal.setUserId(user.getId());
            principal.setUserType(user.getUserType());
            principal.setAgentCode(user.getAgentOfflineCode());
        }

        if(principal.getUserType() == null){
            principal.setUserType(UserType.Unregistered.getCode());
        }

        log.info("User logged in! unionid:{}, userId:{}, userTYpe:{}, name:{}",principal.getUnionid(),
                principal.getUserId(),principal.getUserType(),principal.getName());

        return new SimpleAuthenticationInfo(principal,authenticationToken.getCredentials(),getName());
    }
}
